package cn.com.huzo.webapp.controller.workflow;

import cn.com.huzo.common.util.ApplicationConfiguration;
import huzofw.common.web.controller.BaseFormController;
import huzofw.model.entities.workflow.TaskRoleEntity;
import huzofw.model.entities.workflow.TaskRoleUserEntity;
import huzofw.service.workflow.TaskRoleManager;
import huzofw.service.workflow.TaskRoleUserManager;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 任务角色控制器
 *
 * @author 吴旭
 */
@Controller
public class TaskRoleController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    private final String _rootNodeText = (String) ApplicationConfiguration.getProperty("CompanyName");

    @Resource(name = "TaskRoleManager")
    private TaskRoleManager _manager_role;

    @Resource(name = "TaskRoleUserManager")
    private TaskRoleUserManager _manager_role_user;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到任务角色一览画面
     *
     * @return
     */
    @RequestMapping("workflow/task_role/list.html")
    public ModelAndView redirectListWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("workflow.task_role.list.def");
        return view;
    }

    /**
     * 任务角色编辑画面
     * <p>
     * @param request 上下文请求
     * @param action 操作标志 1;增加 2：编辑
     * @param roleId 任务角色标识
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "workflow/task_role/{action}/{roleId}/edit.html",
            method = {RequestMethod.GET})
    public ModelAndView redirectEditWnd(HttpServletRequest request, @PathVariable(value = "action") int action,
            @PathVariable(value = "roleId") Long roleId) throws Exception {
        // 设置功能号
        //request.setAttribute("ResourceNo", ResourceNo);
        // 设置角色号
        //request.setAttribute("RoleNo", RoleNo);

        ModelAndView view = new ModelAndView();
        view.addObject("action", action);
        view.addObject("roleId", roleId.toString());

        view.setViewName("workflow.task_role.edit.def");
        return view;
    }

    /**
     * 弹出任务角色选择画面
     * <p>
     * @return
     */
    @RequestMapping("workflow/task_role/choice.html")
    public ModelAndView redirectChoicePopupWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.workflow.task_role.choice.def");
        return view;
    }

    /**
     * 转到任务角色在线维护画面
     * <p>
     * @return
     */
    @RequestMapping("workflow/task_role/online/edit.html")
    public ModelAndView redirectTaskRoleOnlineEditWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.workflow.task_role.online_edit.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="任务角色数据处理部分">
    /**
     * 根据标识取得角色数据。
     *
     * @param key 角色标识
     * @return 角色数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "workflow/task_role/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchByKey(@RequestBody Long key) throws Exception {
        _manager_role.setRootNodeText(_rootNodeText);
        return _manager_role.fetchByHeaderKey(key);
    }

    /**
     * 得到全部任务角色分页数据。
     *
     * @param criterion 检索条件
     * @return 任务角色分页数据
     * @throws java.lang.Exception
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "workflow/task_role/fetchForPage.html",
            method = RequestMethod.POST,
            produces = {"application/xml", "application/json"})
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager_role.fetchForPage(criterion);
    }

    /**
     * 增加任务角色。
     *
     * @param instance 要新增的数据
     * @return
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "workflow/task_role/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Long insert(@RequestBody final TaskRoleEntity instance) throws Exception {
        if (instance.getRoleId() != 0) {
            return _manager_role.update(instance).longValue();
        } else {
            return _manager_role.insert(instance);
        }
    }

    /**
     * 删除任务角色。
     *
     * @param criterion 要删除的数据
     * <p>
     * @return
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "workflow/task_role/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final Map<String, Object> criterion) throws Exception {
        _manager_role.setRootNodeText(_rootNodeText);
        return _manager_role.delete(criterion);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="角色所属成员数据处理部分">
    /**
     * 批量增加角色成员。
     *
     * @param itemList 要处理的数据列表
     * @return
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "workflow/task_role/lines/insert.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> batchInsert(@RequestBody final List<TaskRoleUserEntity> itemList) throws Exception {
        return _manager_role_user.batchInsert(itemList, _rootNodeText);
    }

    /**
     * 删除角色所属用户。
     *
     * @param instance 要删除的成员
     * @return 影响行数
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "workflow/task_role/lines/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final TaskRoleUserEntity instance) throws Exception {
        return _manager_role_user.delete(instance, _rootNodeText);
    }

    /**
     * 删除角色所属用户。
     *
     * @param itemList 要删除的成员
     * @return 影响行数
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "workflow/task_role/lines/batch_delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> batchDelete(@RequestBody final List<TaskRoleUserEntity> itemList) throws Exception {
        return _manager_role_user.batchDelete(itemList, _rootNodeText);
    }

    /**
     * 根据角色标识取得所属用户。
     *
     * @param roleKey 角色标识
     * @return 角色所属用户
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "workflow/task_role/lines/fetchUserByRoleKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<TaskRoleUserEntity> fetchUserByRoleKey(@RequestBody final Long roleKey) throws Exception {
        return _manager_role_user.fetchUserByRoleKey(roleKey);
    }
    // </editor-fold>
}
